// 获取环境变量的值
const project_name = 'EXCEL';

const config = {
    // wx: get_sys_config(),
    mysql: get_mysql_config(),
    // mongo: get_mongo_config(),
    // minio: get_minio_config(),
    // redis: get_redis_config()
};

// 获取小程序配置
function get_sys_config() {
    let appid = process.env[`${project_name}_WX_APPID`];
    let secret = process.env[`${project_name}_WX_SECRET`];
    if (!appid || !secret) {
        console.log('读取不到系统环境变量:');
        console.log(`    - ${project_name}_WX_APPID`);
        console.log(`    - ${project_name}_WX_SECRET`);
        process.exit(1);
    }
    return { appid, secret };
}

// 获取mysql配置
function get_mysql_config() {
    let host = process.env[`${project_name}_MYSQL_HOST`];
    let port = process.env[`${project_name}_MYSQL_PORT`];
    let user = process.env[`${project_name}_MYSQL_USER`];
    let pswd = process.env[`${project_name}_MYSQL_PSWD`];
    let database = process.env[`${project_name}_MYSQL_DB`];
    if (!host || !port || !user || !pswd || !database) {
        console.log('读取不到系统环境变量:');
        console.log(`    - ${project_name}_MYSQL_HOST`);
        console.log(`    - ${project_name}_MYSQL_PORT`);
        console.log(`    - ${project_name}_MYSQL_USER`);
        console.log(`    - ${project_name}_MYSQL_PSWD`);
        console.log(`    - ${project_name}_MYSQL_DB`);
        process.exit(1);
    }
    return { host, port, user, pswd, database };
}

// 获取redis配置
function get_redis_config() {
    let host = process.env[`${project_name}_REDIS_HOST`];
    let port = process.env[`${project_name}_REDIS_PORT`];
    let pswd = process.env[`${project_name}_REDIS_PSWD`];
    if (!host || !port || !pswd) {
        console.log('读取不到系统环境变量:');
        console.log(`    - ${project_name}_REDIS_HOST`);
        console.log(`    - ${project_name}_REDIS_PORT`);
        console.log(`    - ${project_name}_REDIS_PSWD`);
        process.exit(1);
    }
    return { host, port, pswd };
}

// 获取MongoDB配置
function get_mongo_config() {
    let host = process.env[`${project_name}_MONGO_HOST`];
    let port = process.env[`${project_name}_MONGO_PORT`];
    let user = process.env[`${project_name}_MONGO_USER`];
    let pswd = process.env[`${project_name}_MONGO_PSWD`];
    if (!host || !port || !user || !pswd) {
        console.log('读取不到系统环境变量:');
        console.log(`    - ${project_name}_MONGO_HOST`);
        console.log(`    - ${project_name}_MONGO_PORT`);
        console.log(`    - ${project_name}_MONGO_USER`);
        console.log(`    - ${project_name}_MONGO_PSWD`);
        process.exit(1);
    }
    return { host, port, user, pswd };
}

// 获取MinIO配置
function get_minio_config() {
    let endPoint = process.env[`${project_name}_MINIO_ENDPOINT`];
    let port = process.env[`${project_name}_MINIO_PORT`];
    let useSSL = process.env[`${project_name}_MINIO_USESSL`];
    let accessKey = process.env[`${project_name}_MINIO_AKEY`];
    let secretKey = process.env[`${project_name}_MINIO_SKEY`];
    if (!endPoint || !port || !useSSL || !accessKey || !secretKey) {
        console.log('读取不到系统环境变量:');
        console.log(`    - ${project_name}_MINIO_ENDPOINT`);
        console.log(`    - ${project_name}_MINIO_PORT`);
        console.log(`    - ${project_name}_MINIO_USESSL`);
        console.log(`    - ${project_name}_MINIO_AKEY`);
        console.log(`    - ${project_name}_MINIO_SKEY`);
        process.exit(1);
    }
    return { endPoint, port, useSSL, accessKey, secretKey };
}

module.exports = config;